/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

'Gio.Core.Widget.Loader'.namespace();

/**
 * Gio.Core.Widget.Loader
 * This class required plugins:
 * - ajaxq
 * - jquery.json
 */
Gio.Core.Widget.Loader = function() {};

Gio.Core.Widget.Loader.userUrl = '';
Gio.Core.Widget.Loader.baseUrl = '/';
Gio.Core.Widget.Loader.cacheEnable = 'false';
Gio.Core.Widget.Loader.cacheTimout = 0;

/**
 * Put the widget loading to queue
 * 
 * @param string module Name of module
 * @param string name Name of widget
 * @param string data Parameters names and values in JSON format
 * @param string container The DIV container that render the widget
 * @param string callback Function or name of callback function (since 2.0.8)
 * @return void
 */
Gio.Core.Widget.Loader.queue = function(module, name, data, container, callback) {
	Gio.Core.Widget.Loader.queueAction(module, name, 'show', data, container, callback);
};

/**
 * Put the widget action to queue
 * 
 * @param string module Name of module
 * @param string name Name of widget
 * @param string act Action's name
 * @param string data Parameters names and values in JSON format
 * @param string container The DIV container that render the widget
 * @param string callback Function or name of callback function (since 2.0.8)
 * @return void
 */
Gio.Core.Widget.Loader.queueAction = function(module, name, act, data, container, callback) {
	$(container).addClass('loading');
	var baseUrl = Gio.Core.Widget.Loader.baseUrl;
	baseUrl     = baseUrl.replace(/\/+$/, '');
	var url = (Gio.Core.Widget.Loader.userUrl != '') ? Gio.Core.Widget.Loader.userUrl : baseUrl + '/core/widget/ajax'; 
	$.ajaxq('widget_loader', {
		url: url,
		type: 'POST',
		data: { mod: module, name: name, act: act, cache: Gio.Core.Widget.Loader.cacheEnable, timeout: Gio.Core.Widget.Loader.cacheTimeout, params: data },
		success: function(response) {
			var ret = $.evalJSON(response);
			if (container != null) {
				$(container).removeClass('loading')
							.html(ret.content);
			}
			
			for (var i in ret.cssFiles) {
				if ($('head').find('link[type="text/css"][href="' + ret.cssFiles[i] + '"]').length == 0) {
					$('<link rel="stylesheet" type="text/css" href="' + ret.cssFiles[i] + '" />').appendTo('head');
				}
			}
			
			for (i in ret.jsFiles) {
				var file = ret.jsFiles[i];
				if (file != null
					&& $('head').find('script[type="text/javascript"][src="' + file + '"]').length == 0
					&& $('body').find('script[type="text/javascript"][src="' + file + '"]').length == 0) {
					$('<script type="text/javascript" src="' + file + '"></script>').appendTo('body');
				}
			}
			
			if (callback) {
				callback(ret.content);
			}
			
			for (i in ret.jsScripts) {
				/**
				 * Add scripts to the end of page
				 */
				var script = ret.jsScripts[i];
				if (script != null) {
					var scriptId = [module, name, act].join('_');
					$('body').find('script[type="text/javascript"][id="' + scriptId + '"]').remove();
					
					var s  = document.createElement('script');
					s.type = 'text/javascript';
					s.id   = scriptId;
					s.text = script;
					document.body.appendChild(s);
				}
			}
		}
	});
};